package com.genius.adminmanager.menu.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.menu.entity.MenuJson;
import com.genius.adminmanager.menu.service.MenuService;
import com.genius.adminmanager.other.entity.ModeJson;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.workflow.page.PageResult;
import com.genius.workflow.page.SqlLang;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBTools2;
import com.genius.workflow.util.DBUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

import java.sql.SQLException;

@Controller
public class MenuCommonController {

	
	
	@PostMapping("/adminmanager/menu/queryMenuField")
	@ResponseBody
	public JSONObject queryMenuField(@RequestBody JSONObject json, HttpSession session){
		JSONObject response = new JSONObject();
    	response.put(ResponseJson.code, 0);
    	String field = MenuJson.field;
    	String table = "t_menu_field";
    	String whereField = MenuJson.menuId;
    	String sql = DBTools.getSelectAndWhereSql(field, table, whereField);
    	String [] parms = DBTools.getSelectParms(whereField, json);
    	try {
			JSONObject fieldJson = DBUtils.select(sql, parms);
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.data, fieldJson==null?new JSONObject():fieldJson);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Query_Data_Error);
		}
    	return response;
	}
	
	/**
	 * 导入平台的模块菜单
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/adminmanager/menu/loadPlatformModel")
	@ResponseBody
    public JSONObject loadPlatformModel(@RequestBody JSONObject json,HttpSession session){
        JSONObject response = new JSONObject();
        response.put(ResponseJson.code, 0);
        int limit = json.getIntValue(PageResult.limit);
        int page = json.getIntValue(PageResult.page);
        SqlLang sql = null;
        String headField = ModeJson.moduleId+","+ModeJson.moduleName+","+ModeJson.moduleUrl+","+ModeJson.field;
        String selectSB = "select "+headField+" from t_mode";
        
        String asc = null;
        String ascField = null;
        sql = DBTools2.getSelectPageSql(headField,selectSB,page,limit,ascField,asc);
        try {
            PageResult result = MenuService.getBodyList(sql);
            
            JSONArray data = (JSONArray)result.getData();
            response.put(ResponseJson.count, result.getCount());
            response.put(ResponseJson.data, data);
            
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            
        } catch (ConnectionException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
        } catch (DBClosedException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
        } catch (SQLException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_Query_Data_Error);
        }
        return response;
    }
	
	@PostMapping("/adminmanager/menu/updateMenuField")
	@ResponseBody
	public JSONObject updateMenuField(@RequestBody JSONObject json, HttpSession session){
		JSONObject response = new JSONObject();
    	response.put(ResponseJson.code, 0);
    	String updateField = MenuJson.field;
    	String table = "t_menu_field";
    	String whereField = MenuJson.menuId;
    	
    	try {
    		DBTools.update(updateField, whereField, table, json);
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.data, json);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Error);
		}
    	return response;
	}

	@GetMapping("/adminmanager/menu/toIcons")
	public String toIcons(){
		return "/views/adminmanager/menu/icons";
	}
}
